const router = require('koa-router')(),
      mysql = require('mysql2/promise'),
      { community } = require('../models'),
      log = console.log;

router.prefix('/api/kdoing/community');

router.get('/getcommunity', async (ctx, next) => {
  var { comm_id } = ctx.params;
  ctx.body = await community.getCommunity(comm_id);
});
router.get('/getusercommunity/:user_id', async (ctx, next) => {
  var { user_id } = ctx.params;
  ctx.body = await community.getUserCommunity(user_id);
});
router.get('/getcommbypage', async (ctx, next) => {
  var { page,limit } = ctx.query;
  ctx.body = await community.getCommByPage(page,limit);
});
router.get('/getcommimg/:comm_id', async (ctx, next) => {
  var { comm_id } = ctx.params;
  ctx.body = await community.getCommimgByCommid(comm_id);
});
router.get('/getreviews/:comm_id', async (ctx, next) => {
  var { comm_id } = ctx.params;
  ctx.body = await community.getReviewsByCommid(comm_id);
});
router.post('/addreview', async (ctx, next) => {
  var { user_id, comm_id, review_text } = ctx.request.body;
  ctx.body = await community.addReview(user_id, comm_id, review_text);
});
router.post('/addcomm', async (ctx, next) => {
  var {comm_id, comm_text, user_id} = ctx.request.body;
  ctx.body = await community.addComm(comm_id,comm_text, user_id);
});
router.post('/addcommimg', async (ctx, next) => {
  var {comm_id, commimg_src} = ctx.request.body;
  ctx.body = await community.addCommimg(comm_id, commimg_src);
});
router.get('/getcommid', async (ctx, next) => {
  ctx.body = await community.getCommid();
})
router.delete('/delcommbycommid/:comm_id', async (ctx, next) => {
  var { comm_id } = ctx.params;
  ctx.body = await community.delCommByCommid(comm_id); 
});
router.delete('/delreviewbyreviewid/:review_id', async (ctx, next) => {
  var { review_id } = ctx.params;
  ctx.body = await community.delReviewByReviewid(review_id); 
});
router.get('/getlikes/:comm_id', async (ctx, next) => {
    var { comm_id } = ctx.params;
  ctx.body = await community.getLikes(comm_id);
})
router.post('/addlike', async (ctx, next) => {
  var {comm_id, user_id} = ctx.request.body;
  ctx.body = await community.addLike(comm_id, user_id);
});
router.delete('/dellike', async (ctx, next) => {
  var {comm_id, user_id} = ctx.request.body;
  ctx.body = await community.delLike(comm_id, user_id); 
});
router.get('/getlikelist', async (ctx, next) => {
  ctx.body = await community.getLikelist();
})


module.exports = router;
